package com.ktjiaoyu.controller;

import com.ktjiaoyu.pojo.User;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("msg", "hello thymeleaf");
        return "hello";
    }

    @GetMapping("/expression")
    public String expression(Model model,
                             HttpServletRequest request, HttpSession session) {
        //存储数据到model
        model.addAttribute("name", "zhangsan");
        model.addAttribute("user", new User(1, "lisi", "男", 18));

        //存储数据到request和session
        request.setAttribute("username", "lisi");
        session.setAttribute("username", "wangwu");

        model.addAttribute("date", new Date());

        return "demo/expression";
    }

    @GetMapping("/expression2")
    public String expression2(Model model) {
        model.addAttribute("user", new User(1, "lisi", "男", 18));
        return "demo/expression2";
    }

    @GetMapping("/link")
    public String link() {
        return "demo/link";
    }

    @GetMapping("/insert")
    @ResponseBody
    public User insert(User user) {
        return user;
    }

    @GetMapping("/properties")
    public String properties(Model model) {
        model.addAttribute("method","post");
        model.addAttribute("name","username");
        model.addAttribute("value","lisi");
        model.addAttribute("color","color:red");

        return "demo/properties";
    }

    @GetMapping("/ifunless")
    public String ifunless(Model model){
        model.addAttribute("age",15);
        return "demo/ifunless";
    }

    @GetMapping("/switch2")
    public String switch2(Model model){
        model.addAttribute("sex","女2");
        return "demo/switch";
    }

    @GetMapping("/each")
    public String each(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","女",19));
        list.add(new User(3,"王五","女",20));
        list.add(new User(4,"赵六","男",21));

        model.addAttribute("list",list);

        return "demo/each";
    }

    @GetMapping("/inline")
    public String inline(Model model) {
        model.addAttribute("name", "zhangsan");
        return "demo/inline";
    }

    @GetMapping("/frame")
    public String frame() {
        return "demo/frame";
    }

    @GetMapping("/list")
    public String list(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","女",19));
        list.add(new User(3,"王五","女",20));
        list.add(new User(4,"赵六","男",21));

        model.addAttribute("list",list);

        return "demo/user-list";
    }

}